package com.yang.controller;

import com.yang.mapper.DepartmentMapper;
import com.yang.mapper.TaskMainMapper;
import com.yang.mapper.TaskReplacementMapper;
import com.yang.pojo.Department;
import com.yang.pojo.TaskMain;
import com.yang.pojo.TaskReplacement;
import com.yang.pojo.UserInfo;
import com.yang.service.DepartmentService;
import com.yang.service.UserInfoService;
import com.yang.util.JsonResult;
import com.yang.util.JwtUtil;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * @Author 杨天可
 * @Date 2021/3/30 15:12
 * @Version 1.0
 */
@Transactional
@RestController
@RequestMapping("/taskMain")
public class TaskMainController {

    @Resource
    UserInfoService userInfoService;
    @Resource
    DepartmentService departmentService;
    @Resource
    TaskMainMapper taskMainMapper;
    @Resource
    TaskReplacementMapper taskReplacementMapper;

    /***
     * 新消息总条数
     * @return
     */
    @GetMapping("/newTaskCount")
    public Map<String,Object> newTaskCount(HttpServletRequest request,HttpServletResponse response){
        JsonResult jsonResult = new JsonResult();
        try{
            response.setHeader("Content-type", "application/json;charset=UTF-8");
            String token = request.getHeader("token");
            Integer userInfoId = JwtUtil.getUserInfoId(token);
            UserInfo userInfoDB = userInfoService.findById(userInfoId);

            int NotReadCount = 0;
            //人事管理员
            if(userInfoDB.getRoleId().equals(2)){
                List<TaskMain> infoHrNotRead = taskMainMapper.findInfoHrNotRead();
                NotReadCount = infoHrNotRead.size();
            }else if (userInfoDB.getRoleId().equals(4)){//部门主管
                List<TaskMain> infoDirectorNotRead = taskMainMapper.findInfoDirectorNotRead();
                for (TaskMain info : infoDirectorNotRead){

                    //分类分部门统计
                    if(info.getTaskType().equals(0)){//补卡申请
                        TaskReplacement byId = taskReplacementMapper.findById(info.getId());
                        if(byId.getDirectorId().equals(userInfoId)){
                            NotReadCount++;
                        }
                    }
                }
            }
            jsonResult.setCode("200");
            jsonResult.setData(NotReadCount);
        }catch(Exception e){
            e.printStackTrace();
            jsonResult.setCode("500");
            jsonResult.setMsg("系统开小差");
        }
        return jsonResult.getValues();
    }
}
